<?php
require_once 'model/db.php';
if(isset($_POST["submit"])){
    $min=$_POST["min"];
    $max=$_POST["max"];
    $key=$_POST["key"];
    $apps=$_POST["id_app"];
    if(empty($min))
        $min="";
    else 
        $min="(pages.likes>=$min) AND"; 
    if(empty($max))
        $max="";
    else 
        $max="(pages.likes<=$max) AND";
$query="SELECT * FROM pages
           INNER JOIN apps_pages ON pages.id=apps_pages.id_page
           INNER JOIN apps ON apps_pages.id_app=apps.id 
                WHERE
                    $min
                    $max
                    (pages.description LIKE '%$key%') AND
                    (apps.id='$apps')
                    limit 0,10";       
}
else
$query="SELECT * FROM pages limit 0,15";
var_dump($query);
$pages=  getData($query);
$query="SELECT * FROM apps";
$apps=  getData($query);
?>

<htm>
    <head>
        <title> Doan Van Quoc</title>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    </head>
    <body>
<div id="khung">
    <div id="cout"><?php
    (int) $count1=0;
    (int) $count2=0;
    $count1=getCount("search.txt");
    $count2=getCount("people.txt");
    if(isset($_POST["submit"])){
    $count1++;
    setCount("search.txt", $count1);
    }
    else{
        $count2++;
        setCount("people.txt",$count2);
    }
    ?>
    <?php echo "Search: ".$count1; ?><br>
    <?php echo "People: ".$count2; ?>
    </div>
    <form method="POST">
    <div id="search">
        <div class="tieude">Min: </div>
        <input type="text" name="min">
        <div class="tieude">Max: </div>
        <input type="text" name="max">
        <div class="tieude">Key: </div>
        <input type="text" name="key">
        <br>
        Apps: <br>
        <select name="id_app">
            <?php foreach ($apps as $app): ?>
            <option value="<?php echo $app["id"]?>"><?php echo $app["name"]; ?></option>
            <?php endforeach;?>
        </select>
        <br>
        <input type="submit" name="submit" value="search">    
    </div>
    </form>
    <div id="show">
        <?php if(!empty($pages)):?>
        <table>
            <tr class="header-show">
                <td>Name</td>
                 
                <td>Like</td>
                <td>Status</td>
                <td>Description</td>
            </tr>
            <?php foreach ($pages as $page): ?>
            <tr>
                <td><?php echo $page["name"]?></td>
                <td><?php echo $page["likes"]?></td>
                <td>
                    <?php if($page["status"]==-1) 
                                echo "Detele"; 
                          else if($page["status"]==1) 
                                echo "Action"; 
                          else 
                                echo "Unknown"; ?>
                </td>
                <td><?php echo substr($page["description"],0,100);?></td>
            </tr>
            <?php endforeach;?>
        </table>
        <?php else : ?>
        <div id="no_search"> No sreach. </div>
        <?php endif;?>
    </div>
    
</div>
</body>
</htm>